/*
 * Text.java
 *
 * Created on August 16, 2007, 12:12 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.app.client;

import java.util.Iterator;
import org.infoset.xml.Element;
import org.infoset.xml.Name;

/**
 *
 * @author alex
 */
public class Text
{
   static final String TEXT = "text";
   static final String HTML = "html";
   static final String XHTML = "xhtml";

   Element content;
   
   /** Creates a new instance of Text */
   public Text(Element content)
   {
      this.content = content;
   }
   
   public String getType() {
      return content.getAttributeValue("type");
   }
   
   public boolean isXML() {
      String type = getType();
      if (type==null) {
         return false;
      }
      if (XHTML.equals(type)) {
         return true;
      } else {
         return type.endsWith("/xml") || type.endsWith("+xml");
      }
   }
   
   public void setXML(Element child) {
      content.setAttributeValue("type","text/xml");
      content.clear();
      content.add(child);
   }
   
   public void setXHTML(Element child) {
      content.setAttributeValue("type","xhtml");
      content.clear();
      content.add(child);
   }
   
   public void setText(String text)
   {
      setText(null,text);
   }
   public void setText(String type,String text)
   {
      if (type==null) {
         content.getAttributes().remove(Name.create("type"));
      } else {
         content.setAttributeValue("type",type);
      }
      content.clear();
      content.addCharacters(text);
   }
   
   public String getText() {
      return content.getText();
   }

   public Element getContent() {
      Iterator<Element> children = content.getElementChildren();
      return children.hasNext() ? children.next() : null;
   }
   
   public String getSourceLink() {
      return content.getAttributeValue("src");
   }
   
   public void setSourceLink(String type,String src)
   {
      content.clear();
      if (type!=null) {
         content.setAttributeValue("type",type);
      }
      content.setAttributeValue("src",src);
   }
   
   public Element getElement() {
      return content;
   }
   
}
